package org.anyin.gitee.cloud.lib.permission;

import cn.dev33.satoken.stp.StpInterface;
import org.anyin.gitee.cloud.lib.core.cache.CacheTemplate;
import org.anyin.gitee.cloud.lib.core.utils.CollectionUtil;

import java.util.List;

public class StpInterfaceImpl implements StpInterface{

    private final PermissionRoleService permissionService;

    private final CacheTemplate cacheTemplate;


    public StpInterfaceImpl(PermissionRoleService permissionService, CacheTemplate cacheTemplate) {
        this.permissionService = permissionService;
        this.cacheTemplate = cacheTemplate;
    }


    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String key = PermissionRedisConstant.DEFAULT_USER_PERMISSION_PREFIX + loginId;
        List<String> permissions = cacheTemplate.valueGetList(key, String.class);
        if(CollectionUtil.isEmpty(permissions)){
            permissions = permissionService.getPermissionByLoginId((String) loginId);
            cacheTemplate.valueSet(key, permissions);
        }
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String key = PermissionRedisConstant.DEFAULT_USER_ROLE_PREFIX + loginId;
        List<String> roles = cacheTemplate.valueGetList(key, String.class);
        if(CollectionUtil.isEmpty(roles)){
            roles = permissionService.getRoleByLoginId((String) loginId);
            cacheTemplate.valueSet(key, roles);
        }
        return roles;
    }

}
